********************************************************
*											   		   *
*		Title: 6_survival_rate_metal_paper_cotton	   *		
*													   *
********************************************************


/*  ================================  NOTES  ================================

+ PURPSOSE:
	
	In this do-file we create table 2 that analyses the survival rate 
			
+ Inputs:	Located in data/processed

		* 1. panel_metallurgy.dta
		* 2. panel_cotton.dta
		* 3. panel_paper.dta
			 
==============================  TOP MATTER ==============================*/



************** END HEADER **************


	*set paths
	global proc_data "`pdir'/data"
	global output "`pdir'/outputs/main_paper"


******************************** END HEADER ********************************


*********************************************************
*****************		Metallurgy	 	*****************
*********************************************************

use "$proc_data/panel_metallurgy.dta", clear 


* see the survival results
tab survival, matcell(met1)

* save the total number of plants that survived  
scalar surv_met_1 = met1[2,1]

*save the total number of plants
scalar total_firms_met_1 = met1[1,1] + met1[2,1]

*save the perc of survival 
scalar perc_survival_met_1 = (surv_met_1/total_firms_met_1)

display perc_survival_met_1
display total_firms_met_1


* Now get the survival statistics for the "restricted sample survival rate" 
* uses only the subset of plants located in communes that had only one plant 
* in the initial period. 

keep if single_plant == 1

* see the survival results
tab survival, matcell(met2)

* save the total number of plants that survived  
scalar surv_met_2 = met2[2,1]

*save the total number of plants
scalar total_firms_met_2 = met2[1,1] + met2[2,1]

*save the perc of survival 
scalar perc_survival_met_2 = (surv_met_2/total_firms_met_2)

display perc_survival_met_2
display total_firms_met_2


* table for metal survival 
mat surv = J(5,3,.)
mat surv[1,2] = perc_survival_met_1*100
mat surv[2,2] = total_firms_met_1

mat surv[4,2] = perc_survival_met_2*100
mat surv[5,2] = total_firms_met_2


*********************************************************
*******************		  Paper	 	  *******************
*********************************************************
	
	
use "$proc_data/panel_paper.dta", clear

* see the survival results
tab survival, matcell(pap1)

* save the total number of plants that survived  
scalar surv_pap_1 = pap1[2,1]

*save the total number of plants
scalar total_firms_pap_1 = pap1[1,1] + pap1[2,1]

*save the perc of survival 
scalar perc_survival_pap_1 = (surv_pap_1/total_firms_pap_1)

display perc_survival_pap_1
display total_firms_pap_1


* Now get the survival statistics for the "restricted sample survival rate" 
* uses only the subset of plants located in communes that had only one plant 
* in the initial period. 

keep if single_plant == 1

* see the survival results
tab survival, matcell(pap2)

* save the total number of plants that survived  
scalar surv_pap_2 = pap2[2,1]

*save the total number of plants
scalar total_firms_pap_2 = pap2[1,1] + pap2[2,1]

*save the perc of survival 
scalar perc_survival_pap_2 = (surv_pap_2/total_firms_pap_2)

display perc_survival_pap_2
display total_firms_pap_2


* table for metal survival 

mat surv[1,3] = perc_survival_pap_1*100
mat surv[2,3] = total_firms_pap_1

mat surv[4,3] = perc_survival_pap_2*100
mat surv[5,3] = total_firms_pap_2


************************************************************
********************		Cotton	 	********************
************************************************************
	
use "$proc_data/panel_cotton.dta", clear

* see the survival results
tab survival, matcell(cot1)

* save the total number of plants that survived  
scalar surv_cot_1 = cot1[2,1]

*save the total number of plants
scalar total_firms_cot_1 = cot1[1,1] + cot1[2,1]

*save the perc of survival 
scalar perc_survival_cot_1 = (surv_cot_1/total_firms_cot_1)

display perc_survival_cot_1
display total_firms_cot_1


* Now get the survival statistics for the "restricted sample survival rate" 
* uses only the subset of plants located in communes that had only one plant 
* in the initial period. 

keep if single_plant == 1

* see the survival results
tab survival, matcell(cot2)

* save the total number of plants that survived  
scalar surv_cot_2 = cot2[2,1]

*save the total number of plants
scalar total_firms_cot_2 = cot2[1,1] + cot2[2,1]

*save the perc of survival 
scalar perc_survival_cot_2 = (surv_cot_2/total_firms_cot_2)

display perc_survival_cot_2
display total_firms_cot_2


* table for metal survival 

mat surv[1,1] = perc_survival_cot_1*100
mat surv[2,1] = total_firms_cot_1

mat surv[4,1] = perc_survival_cot_2*100
mat surv[5,1] = total_firms_cot_2

************************************************************
********************		Table 2	 	********************
************************************************************
	
frmttable using "$output/Tables/Table_2.tex", statmat(surv) replace ///
	landscape tex fragment ///
	rtitles( "Survival rate" \ "Number of plants" \ "" \ ///
			"Restricted sample survival rate" \ "Number of plants") ///
	ctitles("" "Spinning" "Metallurgy" "Paper milling" \ ///
			"" "1806-1840" "1811-1840" "1794-1840") ///
			sdec(1 \ 0 \ 0 \ 1 \ 0) 
		
** More cleaning for the tex table 

clear
set obs 1
local tex "$output/Tables/Table_2.tex"
local tex2 "$output/Tables/Table_2.tex"

generate strL s = fileread("`tex'") if fileexists("`tex'")
assert filereaderror(s)==0

*removing the beggining and end to only insert the numbers and rows in stata

replace s = ustrregexrf(s,"(?s).*Survival rate","Survival rate")

replace s = ustrregexrf(s,"\\noalign.*(?s)","")
replace s = ustrregexrf(s,"\\end.*(?s)","")

* Adding the % to the Survival rate rows 
replace s = ustrregexra(s,"& 5.0 & 37.7 & 10.8","& 5.0\\% & 37.7\\% & 10.8\\%")
replace s = ustrregexra(s,"12.6 & 52.5 & 24.6","12.6\\% & 52.5\\% & 24.6\\%")

di s

gen byte fw = filewrite("`tex2'",s,1)
					
*remaining formatting of space fixed by hand after generating the Table					
					

					
					
			
